package controller;

import controller.thread_pool_solver.ThreadPool;
import user_interface.FBUserInterface;
import model.SimpleModel;

public class SimpleController {
	// Main data
	private SimpleModel model;
	private FBUserInterface user_interface = null;
	private ThreadPool thread_pool = null;
	// Mutex
	private Object general_mutex = new Object();
	
	public SimpleController(SimpleModel model) {
		this.model = model;		
	}
	
	public void openFile(String fileName) {
		synchronized (general_mutex) {
			try {
				model.openFile(fileName);
			} catch (Exception ex) {
				user_interface.print2console("Error: " + ex.getMessage());
			}
		}
	}
		
	public void calculate(int newMaxIteration, int newNeighborhoodSize, int newTabooListLength, int newRandomSwappingNumber, float newSwappingProportion) {
		synchronized (general_mutex) {
			if(thread_pool != null)
				thread_pool.calculate(newMaxIteration, newNeighborhoodSize, newTabooListLength, newRandomSwappingNumber, newSwappingProportion);
		}
	}
	
	public void pause(boolean state) {
		synchronized (general_mutex) {
			if(thread_pool != null)
				thread_pool.pause(state);			
		}
	}
		
	public void stop() {
		synchronized (general_mutex) {
			if(thread_pool != null)
				thread_pool.stop();			
		}		
	}
	
	public void shutdown() {
		stop();
	}
	
	public void setUserInterface(FBUserInterface userInterface) {
		synchronized (general_mutex) {
			user_interface = userInterface;
			thread_pool = new ThreadPool(model, user_interface);
			Thread t = new Thread(thread_pool);
			t.setDaemon(true);
			t.start();
		}
	}
}
